package com.itheima.home_work.math_topic;

import java.util.ArrayList;

/**
 * 题库
 */
public class QuestionBank {
    private ArrayList<MathTest> mathTests = new ArrayList<>();

    public ArrayList<MathTest> getMathTests() {
        return mathTests;
    }

    public void setMathTests(ArrayList<MathTest> mathTests) {
        this.mathTests = mathTests;
    }

    public int getQuestionNumber() {
        return this.mathTests.size();
    }

    /**
     * 检查答案
     * @param mathTest
     * @param answer
     * @return 是否准确
     */
    public boolean checkQuestion(MathTest mathTest, int answer) {
        return answer == mathTest.calculate();
    }

    /**
     * 添加题目
     * @param mathTest
     */
    public void addQuestion(MathTest mathTest) {
        this.mathTests.add(mathTest);
        System.out.println("成功添加题目: " + mathTest.getQuestion());
        this.showQuestions();
    }

    public void addQuestionNoTip(MathTest mathTest) {
        this.mathTests.add(mathTest);
    }

    public MathTest getTestBlank(int testType) {
        switch (testType) {
            case 0:
                return new AddTest();
            case 1:
                return new SubtractTest();
            case 2:
                return new MultiplyTest();
            case 3:
                return new DivideTest();
        }
        return new AddTest();
    }

    public void removeQuestionByIndex(int index) {
        if (index <= this.mathTests.size()) {
            System.out.println("题目：" + this.mathTests.get(index - 1).getQuestion() + " 成功删除! ");
            this.mathTests.remove(index - 1);
        } else {
            System.out.println("序号有误！不能删除");
        }
    }

    /**
     * 删除题目
     * @param mathTest
     */
    public void removeQuestion(MathTest mathTest) {
        System.out.println("已删除题目: " + mathTest.getQuestion());
        this.mathTests.remove(mathTest);
        this.showQuestions();
    }

    /**
     * 显示所有题目
     */
    public void showQuestions() {
        System.out.println("当前题库如下：");
        for (int i = 0; i < this.mathTests.size(); i++) {
            MathTest mathTest = this.mathTests.get(i);
            System.out.println("问题" + (i + 1) + ": " + mathTest.getQuestion() + "\t\t参考答案：" + mathTest.calculate());
        }
    }
}
